﻿<Settings>
  <setting name="JsonRules">
    <value>
      <![CDATA[
      {
        "RulesForEntities": [
          {
              "Key": "contact", "Value":
                {
                    "EntityName": "contact", "Rules": [
                      {
                        "Name": "sum Contact.annualincome to parent Account.revenue. Validate calculated subtotal agains a target field in Account record. No Formulas, no addtl filtering.",
                        "SourceSetting": {
                          "FieldName": "annualincome",
                          "Relationship": { "ForeignKeyName": "parentcustomerid" },
							            "ReactToAnyFieldChange": "false",
                          "RollupOperation": { 
                            "OperationType": "Sum", 
                            "FieldValidators": [
                            {
                                "Name": "During Rollup, Validate calculated subtotal of Contact.annualincome against parent Account.CreditLimit value. If invalid, send email and stop operation",
                                "ValidatedFieldIsInRelatedRecord": "false",
								                "ThrowExceptionWhenInvalid": "true",
								                "AdditionalErrorMessage": "Subtotal of all Contact.AnnualIncome values exceeds maximum allowed amount for parent Account.CreditLimit",
                                "Action": {
                                    "ActionKind": "SendEmail",
                                    "EntityLogicalName": "contact"
                                },
                                "Range": {
                                    "RangeValidationKind": "Max",
                                    "MaxValue": {
                                        "CrmFieldType": "CrmMoney",
                                        "Name": "account.creditlimit",
                                        "Value": 0,
                                        "DynamicValue": "true",
                                        "ParentEntityFetchQuery": "<fetch distinct='false' mapping='logical' aggregate='false'> <entity name='contact'><filter type='and'><condition attribute='contactid' operator='eq' value='<PK_VALUE>' /> </filter> <link-entity name='account' to='parentcustomerid' from='accountid' alias='account' link-type='inner'>  <attribute name='creditlimit'/></link-entity></entity> </fetch>"
                                    }
                                }
							}]
                          }
                        },
                        "TargetSetting": {
                            "EntityName": "account",
                            "PrimaryKeyName": "accountid",
                            "RollupFieldName": "revenue"
                        }
                      }]
                }
          }]
    }
      ]]>
    </value>
  </setting>
</Settings>